<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use app\model\config as ConfigModel;

/**
 * 判断是否登录
 * sunxiaodong
 */
class Check extends Controller {

    public function __construct() {

        parent::__construct();
        $this->checklogin();
        $this->checkrole();
        $this->myconfig();
    }

    public function checklogin() {

        if (empty(session("u_rid"))) {
            $this->redirect("/register");
        }

        $nt = time();
        $dt = session('time');
        if ($dt == "") {
            $dt = 999999999999999;
        }
//        var_dump($dt);
        if ($nt - $dt > 7200) {
            header("Content-type: text/html; charset=utf-8");
            session(null);
            echo "<script>alert('长时间未操作，请重新登录！');</script>";
            echo "<script>location.href='/register';</script>";
            exit();
        } else {
            session("time", time());
        }
    }

    public function checkrole() {
        $uid = session("u_id");
        $controller_name = request()->controller();
        $action = request()->action();
        $authid = db("auth")->where("auth_c='$controller_name' and auth_a='$action'")->value("auth_id");

        $urid = Db::name('users')->where("u_id=$uid")->value("u_rid");

        $menu = db("role")->where("r_id=$urid")->value("r_menu");
        $newmenu = explode(",", $menu);
        //var_dump($authid);

        if ($authid == "") {
            
        } else {
            if (in_array($authid, $newmenu)) {
//                echo '有权限';
            } else {
                $this->error('你无权限操作！');
            }
        }
    }

    public function myconfig() {
        $list = ConfigModel::get(1)->toArray();
        $this->assign("logos", $list);
    }

}
